package com.parktech.sun.web.controller.management.template;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.email.EmailTemplate;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.sunhz.EmailTemplateService;
import com.parktech.sun.web.controller.management.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: guo
 * Date: 11-1-10
 * Time: 下午9:58
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/management/template")
public class EmailTemplateController extends BaseController{
    @Autowired
    private EmailTemplateService emailTemplateService;

    private final String redirectListURL = "redirect:listEmailTemplates";

    @RequestMapping(value = "/listEmailTemplates", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView listEmailTemplates(HttpServletRequest request) {
        Integer currentPageNo = request.getParameter("pageNo") != null ? Integer.parseInt(request.getParameter("pageNo").trim()) : 1;
        ModelAndView modelAndView = new ModelAndView("management/template/emailTemplateList");

        List fieldColumns = new ArrayList();
        List searchValues = new ArrayList();
        if (request.getParameter("searchKeyName") != null) {
            fieldColumns.add("name");
            searchValues.add(request.getParameter("searchKeyName"));
        }
        Page<EmailTemplate> returnPage = new Page<EmailTemplate>(Constants.PAGINATION_PAGE_SIZE, currentPageNo);
        returnPage.setTotalCount(emailTemplateService.getTotalSizeWithSimilarCondition(EmailTemplate.class, fieldColumns, searchValues));
        returnPage.setResult(emailTemplateService.getPaginationObjectsWithSimilarCondition(EmailTemplate.class, currentPageNo, Constants.PAGINATION_PAGE_SIZE, fieldColumns, searchValues, null, null));

        return modelAndView.addObject(returnPage);
    }


    //    @ModelAttribute      @RequestParam
    @RequestMapping(value = "/manageEmailTemplate", method = RequestMethod.GET)
    public String manageEmailTemplate(Model model, HttpServletRequest request) {
        Long emailTemplateID = request.getParameter("emailTemplateID") != null ? Long.parseLong(request.getParameter("emailTemplateID").trim()) : null;
        if (emailTemplateID == null)     // request to add the new emailTemplate object
        {
            model.addAttribute(new EmailTemplate());
        } else       // request to update the certain emailTemplate object
        {
            EmailTemplate emailTemplate = emailTemplateService.getBeanByID(emailTemplateID);
            model.addAttribute(emailTemplate);
        }
        return "management/template/emailTemplateManage";
    }

    @RequestMapping(value = "/saveManageEmailTemplate", method = RequestMethod.POST)
    public String saveManageEmailTemplate(@ModelAttribute EmailTemplate emailTemplate,HttpServletRequest request) {
        Long emailTemplateID = emailTemplate.getId();
        emailTemplateService.saveOrUpdateBean(emailTemplate);
        if (emailTemplateID == null) {
            // add the success to create message.
           saveSuccessMessage(request,"创建成功！");
        } else {
            // add the success to edit message.
            saveSuccessMessage(request,"修改成功！");
        }

        return "management/template/emailTemplateManage";
    }


    @RequestMapping(value = "/deleteEmailTemplate", method = RequestMethod.GET)
    public String deleteEmailTemplate(@RequestParam("emailTemplateID") Long emailTemplateID) {
        emailTemplateService.removeBean(emailTemplateID);
        return redirectListURL;
    }

    @RequestMapping(value = "/deleteEmailTemplates", method = RequestMethod.POST)
    public String deleteEmailTemplates(HttpServletRequest request) {
        String[] emailTemplateIDArray = request.getParameter("selectedEmailTemplatesIDArray") == null ? new String[0] : request.getParameter("selectedEmailTemplatesIDArray").trim().split(",");
        for (String emailTemplateID : emailTemplateIDArray) {
            emailTemplateService.removeBean(Long.valueOf(emailTemplateID.trim()));
        }
        return redirectListURL;
    }
}